package com.zr.ssm.controller;

import com.zr.ssm.pojo.ArticalCate;
import com.zr.ssm.pojo.Result;
import com.zr.ssm.service.ArticalCateService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

@AllArgsConstructor
@RestController
@CrossOrigin
@RequestMapping("/my/article")
public class ArticalCateController {

    ArticalCateService articalCateService;

    @GetMapping("/cates")
    public Result getArticalCate(){
        Result result = new Result(0, "query cate ok");
        List<ArticalCate> listCate = articalCateService.getArticalCate();
        result.setData(listCate);
        return result;
    }

    @PostMapping("/addcates")
    public Result addCates(ArticalCate articalCate){
        Result result = new Result(0, "add cate ok");
        Boolean flag = articalCateService.addCates(articalCate);
        if (!flag){
            result.setStatus(1);
            result.setMessage("add cate fail");
        }
        return result;
    }

    @GetMapping("/deletecate/{id}")
    public Result deleteCate(@PathVariable Integer id){
        Result result = new Result(0, "delete cate ok");
        Boolean flag = articalCateService.deleteCate(id);
        if (!flag){
            result.setStatus(1);
            result.setMessage("delete cate fail");
        }
        return result;
    }

    @GetMapping("/cates/{id}")
    public Result getCateInfo(@PathVariable Integer id){
        Result result = new Result(0, "query cateinfo ok");
        ArticalCate cateInfo = articalCateService.getCateInfo(id);
        if (cateInfo == null){
            result.setStatus(1);
            result.setMessage("query cateinfo fail");
        }
        result.setData(cateInfo);
        return  result;
    }

    @PostMapping("/updatecate")
    public Result updateCate(ArticalCate articalCate){
        Result result = new Result(0, "update cateinfo ok");
        Boolean flag = articalCateService.updateCateInfo(articalCate);
        if (!flag){
            result.setStatus(1);
            result.setMessage("update cateinfo ok");
        }
        return result;
    }
}
